package com.atguigu.gulimall.product.service.impl;

        import com.atguigu.gulimall.product.dao.BrandDao;
        import com.atguigu.gulimall.product.dao.CategoryDao;
        import com.atguigu.gulimall.product.entity.BrandEntity;
        import com.atguigu.gulimall.product.entity.CategoryEntity;
        import com.atguigu.gulimall.product.service.BrandService;
        import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Service;

        import java.util.List;
        import java.util.Map;
        import java.util.stream.Collectors;

        import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
        import com.baomidou.mybatisplus.core.metadata.IPage;
        import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
        import com.atguigu.common.utils.PageUtils;
        import com.atguigu.common.utils.Query;

        import com.atguigu.gulimall.product.dao.CategoryBrandRelationDao;
        import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
        import com.atguigu.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Autowired
    BrandDao brandDao;
    @Autowired
    CategoryDao categoryDao;
    @Autowired
    CategoryBrandRelationDao relationDao;
    @Autowired
    BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetai(CategoryBrandRelationEntity categoryBrandRelation) {
        //品牌ID
        Long brandId = categoryBrandRelation.getBrandId();
        //分类ID
        Long catelogId = categoryBrandRelation.getCatelogId();

        //查询品牌名
        BrandEntity brandEntity = brandDao.selectById(brandId);
        //查询分类名
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);

        //设置指定ID的品牌名和分类名
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());

        this.save(categoryBrandRelation);
    }

    /**
     * 更新关联信息
     *
     * @param brandId
     * @param name
     */
    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
        //品牌ID
        relationEntity.setBrandId(brandId);
        //品牌名
        relationEntity.setBrandName(name);
        this.update(relationEntity, new UpdateWrapper<CategoryBrandRelationEntity>()
                .eq("brand_id", brandId)
        );
    }

    @Override
    public void updateCatrgory(Long catId, String name) {
        this.baseMapper.updateCategory(catId, name);
    }

    /**
     * 获取分类关联的品牌
     *
     * @param catId
     * @return
     */
    @Override
    public List<BrandEntity> getBrandsCatId(Long catId) {
        List<CategoryBrandRelationEntity> catelogId = relationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>()
                .eq("catelog_id", catId));
        List<BrandEntity> collect = catelogId.stream().map((item) -> {
            Long brandId = item.getBrandId();
            BrandEntity id = brandService.getById(brandId);
            return id;
        }).collect(Collectors.toList());
        return collect;
    }

}